package com.liux.rabbitmq.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public  static  final  String QUEUE_NAME="hello";

    //发消息
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.59.128");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        //创建连接
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();

            //消费成功，回调接口声明
            DeliverCallback deliverCallback = (var1 , var2) -> {
                System.out.println("接收成功，回调接口");
                System.out.println(new String(var2.getBody()));
            };

            //取消消息消费，回调接口声明
            CancelCallback cancelCallback = var2 -> {
                System.out.println("消息消费被取消");
            };
            //消费者消费消息
            /**
             * 1、消费队列名称
             * 2、消费成功之后，是否自动应答，true自动应答
             * 3、消费者成功消费，回调
             * 4、消费者取消消费，回调
             */
            channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}
